<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Controllers\HandleRedis;
use App\Models\kefu;
use App\Models\u_record;
use App\Models\user;

class UserController extends Controller
{
    public function getExample()
    {
        var_dump(self::$userData);//用户数据
        var_dump(self::$userId);//用户id
        var_dump(self::$userToken);//用户token
        var_dump(self::$client_id);//client_id
        var_dump(self::$request or requestData());//接收client发送过来的数据
    }

    /**
     * 获取用户信息
     */
    public function getUser()
    {
        $this->sendToClient(['code' => 200, 'f' => self::$f, 'data' => self::$userData]);
    }

    /**
     * 返回系统信息
     * @param array $data
     * @param string f
     */
    public function system($data = [])
    {
        $data['time'] = time();
        $data['open_time'] = HandleRedis::getTableOpenTime();//获取所有台子的时间
        $this->sendToClient(['code' => 200, 'f' => self::$f, 'data' => $data]);
    }

    /**
     * 换头像
     * @param int portrait
     * @param string f
     */
    public function changePortrait()
    {
        $portrait = requestData('portrait',1);
        $updateInfo = user::updateUserPortraitById(self::$userId,reset($portrait));
        if($updateInfo) {
            self::$userData['portrait'] = reset($portrait);
            self::flushUserData(self::$userData);
            $this->sendToClient(['code'=>200, 'f' => self::$f ,'data'=>true]);
        }
    }

    /**
     * 换用户名
     * @param string name
     * @param string f
     */
    public function changeUserName()
    {
        $name = requestData('name');
        if(reset($name)) {
            $updateInfo = user::updateUserNameById(self::$userId,reset($name));
            if($updateInfo) {
                self::$userData['name'] = reset($name);
                self::flushUserData(self::$userData);
                $this->sendToClient(['code'=>200, 'f' => self::$f ,'data'=>true]);
            }
        }
    }

    /**
     * 修改密码
     * @param string password
     * @param string f
     */
    public function changeUserPassword()
    {
        $password = requestData('password');
        if(reset($password)) {
            $updateInfo = user::updateUserPasswordById(self::$userId,reset($password));
            if($updateInfo) {
                self::$userData['password'] = reset($password);
                self::flushUserData(self::$userData);
                $this->sendToClient(['code'=>200, 'f' => self::$f ,'data'=>true]);
            }
        }
    }

    /**
     * 根据用户发送的字段和值修改用户信息
     */
    public function changeUserInformation()
    {
        $this->sendToClient([
            'code'=>200, 'f' => self::$f ,'data'=>(bool)user::updateUserInformationById(self::$userId,requestData())
        ]);
    }

    /**
     * 用户投注列表
     * @param string f
     */
    public function getUserRecord()
    {
        $this->sendToClient([
            'code'=>200, 'f' => self::$f ,'data'=>u_record::getUserRecord(self::$userId)
        ]);
    }

    /**
     * 客服
     */
    public function getCustom()
    {
        $this->sendToClient(['code'=>200, 'f' => self::$f ,'data'=>kefu::getKefu()]);
    }

    /**
     * 提取彩金(all_shui满100才能提取)
     */
    public function touchAmount()
    {
        $setBoolean = false;
        //加锁
        if(HandleRedis::addUserLock(self::$userId)){
            //获取用户金额和流水
            $userInfo = user::getUserById(self::$userId,'*');
            if($userInfo['all_shui'] >= self::$waterSetting) {
                $money = intval($userInfo['all_shui'])+intval($userInfo['money']);//合并金额
                user::updateUserMoneyAndAllshuiById(self::$userId,$money);//更新金额
                //用户缓存数据更新
                self::$userData['money'] = $money;
                self::$userData['all_shui'] = 0;
                self::flushUserData(self::$userData);
                $setBoolean = true;
            }
            //删锁
            HandleRedis::delUserLock(self::$userId);
        }
        $this->sendToClient(['code'=>200, 'f' => self::$f ,'data'=>$setBoolean]);
    }

    /**
     * 用户下注
     * @throws \Exception
     */
    public function focus()
    {
        $type = 'user';//标注类型
        $action = 'betRoom';//下注
        $showhao = self::$showhao;//showhao
        $room = self::$room;//房间id
        $content = self::$content;//下注信息
        $portrait = self::$userData['portrait'];//用户头像
        $name = self::$userData['name'];//用户昵称
        $id = self::$userId;//id
        $time = date('H:i:s');
        //加锁
        if(HandleRedis::addUserLock(self::$userId)){
            //获取用户金额和流水
            $userMoney = toInt(user::getUserById(self::$userId,'money'));//获取金额
            $focusContent = matchFocusContent($content);//投注信息['money'=>100,'message'=>'x‘];
            //用户金额条件判断
            if($userMoney>=$focusContent['money']) {
                //更新用户金额
                $updateMoney = floatval($userMoney - $focusContent['money']);//金额
                user::updateUserMoneyById(self::$userId,$updateMoney);
                self::$userData['money'] = $updateMoney;
                self::flushUserData(self::$userData);

                //发送回用户端
                $this->sendToHaveLoginUser(['code'=>200, 'f' => self::$f ,'data'=>compact(
                    'room','showhao','name','portrait','content','action','type','id','time'
                )]);

                //加入投注信息
                HandleRedis::setUserBet($showhao,compact(
                    'room','showhao','name','portrait','content','action','type','id','time'
                ));

                //加入聊天信息
                HandleRedis::setChat($showhao,compact(
                    'room','showhao','name','portrait','content','action','type','id','time'
                ));
            }
            //删锁
            HandleRedis::delUserLock(self::$userId);
        }
    }

    /**
     * 撤销下注
     */
    public function reverseFocus()
    {
        $type = 'user';//标注类型
        $action = 'reverseBetRoom';//撤销下注
        $showhao = self::$showhao;//showhao
        $mode = requestData('mode','all','string');//模式(撤回单个还是撤回所有)
        $room = self::$room;//房间id
        $portrait = self::$userData['portrait'];//用户头像
        $name = self::$userData['name'];//用户昵称
        $id = self::$userId;//id
        $time = date('H:i:s');
        //发送回用户端
        $this->sendToHaveLoginUser(['code'=>200, 'f' => self::$f ,'data'=>compact(
            'room','showhao','name','portrait','action','type','id','time'
        )]);
        //加入投注信息
        HandleRedis::setUserBet($showhao,compact(
            'room','showhao','name','portrait','action','type','id','time','mode'
        ));

    }

    /**
     * 普通聊天
     * @throws \Exception
     */
    public function chat()
    {
        $type = 'user';
        $action = 'chatRoom';//聊天
        $showhao = self::$showhao;//showhao
        $room = self::$room;//房间id
        $content = self::$content;//下注信息
        $portrait = self::$userData['portrait'];//用户头像
        $name = self::$userData['name'];//用户昵称
        $id = self::$userId;//id
        $time = date('H:i:s');
        //保存聊天
        HandleRedis::setChat($showhao,compact(
            'room','showhao','name','portrait','content','type','id','time','action'
        ));
        //发送给所有人
        $this->sendToHaveLoginUser(['code'=>200, 'f' => self::$f ,'data'=>compact('portrait','name','time','content','action','type','id')]);
    }

    /**
     * 获取所有聊天信息(初始化。默认只存储五十条)
     * @param int showhao
     */
    public function getAllChat()
    {
        //处理返回信息
        $data = HandleRedis::getAllChat(self::$showhao);
        foreach ($data as $k=>$v) {
            if(($v['type']=='user') && ($v['id']==self::$userId)) {//自己
                $data[$k]['mode'] = 1;
            } elseif (($v['type']=='user') || ($v['type']=='robot')) {//其他用户
                $data[$k]['mode'] = 2;
            } else {//系统信息
                $data[$k]['mode'] = 3;
            }
        }
        $this->sendToClient([
            'code'=>200,
            'f' => self::$f,
            'data'=>array_reverse(//数组倒序
                getNewArrayByFields([//返回指定的字段
                    'room','showhao','name','portrait','content','action','time','mode'//获取指定的字段
                ],$data)
            )
        ]);
    }
}
